<?php
include_once "BaseMapper.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_AgentBase{
    protected $_id;
    protected $_name;
    protected $_address;
    protected $_province_id;
    protected $_district_id;
    protected $_representavie;
    protected $_phone;
    protected $_agent_cat_id;
    protected $_area_id;
    protected $_created;
    protected $_created_by;
    protected $_modified;
    protected $_modified_by;
    protected $_user_id;
    protected $_charges;
    protected $_order;
    protected $_status;
    protected $_legal_capital;
    protected $_experience;
    protected $_ability_archive;
    protected $_ability_transport;
    protected $_person;
    protected $_agent_status;
    protected $_charges_sell;
    protected $_tax_code;
    protected $_identity_card;
    protected $_birthday;
    protected $_ability_sell;

    public function __construct(array $options = NULL)   
    {   
        if (is_array($options)) {   
            $this->setOptions($options);   
        }   
    }   
    public function __set($name, $value)   
    {   
        $method = "set" . $name;   
        if (("mapper" == $name) || !method_exists($this, $method)) {   
            throw new Exception("Invalid Agent property");   
        }   
        $this->$method($value);   
    }   
    public function __get($name)
    {
        $method = "get" . $name;
        if (("mapper" == $name) || !method_exists($this, $method)) {
            throw new Exception("Invalid Agent property");
        }
        return $this->$method();
    }
    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = "set" . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
    public function setId($value){
        $this->_id=$value;
        return $this;
    }
    public function getId(){return $this->_id;}
    
    public function setName($value){
        $this->_name=$value;
        return $this;
    }
    public function getName(){return $this->_name;}
    
    public function setAddress($value){
        $this->_address=$value;
        return $this;
    }
    public function getAddress(){return $this->_address;}
    
    public function setProvince_Id($value){
        $this->_province_id=$value;
        return $this;
    }
    public function getProvince_Id(){return $this->_province_id;}
    
    public function setDistrict_Id($value){
        $this->_district_id=$value;
        return $this;
    }
    public function getDistrict_Id(){return $this->_district_id;}
    
    public function setRepresentavie($value){
        $this->_representavie=$value;
        return $this;
    }
    public function getRepresentavie(){return $this->_representavie;}
    
    public function setPhone($value){
        $this->_phone=$value;
        return $this;
    }
    public function getPhone(){return $this->_phone;}
    
    public function setAgent_Cat_Id($value){
        $this->_agent_cat_id=$value;
        return $this;
    }
    public function getAgent_Cat_Id(){return $this->_agent_cat_id;}
    
    public function setArea_Id($value){
        $this->_area_id=$value;
        return $this;
    }
    public function getArea_Id(){return $this->_area_id;}
    
    public function setCreated($value){
        $this->_created=$value;
        return $this;
    }
    public function getCreated(){return $this->_created;}
    
    public function setCreated_By($value){
        $this->_created_by=$value;
        return $this;
    }
    public function getCreated_By(){return $this->_created_by;}
    
    public function setModified($value){
        $this->_modified=$value;
        return $this;
    }
    public function getModified(){return $this->_modified;}
    
    public function setModified_By($value){
        $this->_modified_by=$value;
        return $this;
    }
    public function getModified_By(){return $this->_modified_by;}
    
    public function setOrder($value){
        $this->_order=$value;
        return $this;
    }
    public function getOrder(){return $this->_order;}
    
    public function setStatus($value){
        $this->_status=$value;
        return $this;
    }
    public function getStatus(){return $this->_status;}
    
    public function setUser_Id($value){
        $this->_user_id=$value;
        return $this;
    }
    public function getUser_Id(){return $this->_user_id;}
    
    public function setCharges($value){
        $this->_charges=$value;
        return $this;
    }
    public function getCharges(){return $this->_charges;}
    
    public function setLegal_Capital($value){
        $this->_legal_capital=$value;
        return $this;
    }
    public function getLegal_Capital(){return $this->_legal_capital;}
    
    public function setExperience($value){
        $this->_experience=$value;
        return $this;
    }
    public function getExperience(){return $this->_experience;}
    
    public function setAbility_Archive($value){
        $this->_ability_archive=$value;
        return $this;
    }
    public function getAbility_Archive(){return $this->_ability_archive;}
    
    public function setAbility_Transport($value){
        $this->_ability_transport=$value;
        return $this;
    }
    
    public function getAbility_Transport(){return $this->_ability_transport;}
    
    public function setPerson($value){
        $this->_person=$value;
        return $this;
    }
    public function getPerson(){return $this->_person;}
    
    public function setAgent_Status($value){
        $this->_agent_status=$value;
        return $this;
    }
    
    public function getAgent_Status(){return $this->_agent_status;}
    
    public function setCharges_Sell($value){
        $this->_charges_sell=$value;
        return $this;
    }    
    public function getCharges_Sell(){return $this->_charges_sell;}
   
    public function setTax_Code($value){
        $this->_tax_code=$value;
        return $this;
    }    
    public function getTax_Code(){return $this->_tax_code;}
    
    public function setIdentity_Card($value){
        $this->_identity_card=$value;
        return $this;
    }    
    public function getIdentity_Card(){return $this->_identity_card;}
    public function setBirthday($value){
        $this->_birthday=$value;
        return $this;
    }    
    public function getBirthday(){return $this->_birthday;}
    public function setAbility_Sell($value){
        $this->_ability_sell=$value;
        return $this;
    }    
    public function getAbility_Sell(){return $this->_ability_sell;}
}
class Model_DbTable_Agent extends Zend_Db_Table_Abstract{
    protected $_name="agent";
    protected $_primary="id";
}
class Model_AgentMapperBase extends BaseMapper{
    protected $_dbTable;
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception("Invalid table data gateway provided");
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
 
    public function getDbTable()
    {
        if (NULL === $this->_dbTable) {
            $this->setDbTable("Model_DbTable_Agent");
        }
        return $this->_dbTable;
    }
     public function save(Model_Agent $entry){
        $data = array(
               'id' => $entry->getId(),
            'name' => $entry->getName(),
            'address' => $entry->getAddress(),
            'phone' => $entry->getPhone(),
            'province_id' => $entry->getProvince_Id(),
            'district_id' => $entry->getDistrict_Id(),
            'representavie' => $entry->getRepresentavie(),
            'agent_cat_id' => $entry->getAgent_Cat_Id(),
            'charges' => $entry->getCharges(),
            'user_id' => $entry->getUser_Id(),
            'created' => $entry->getCreated(),
            'created_by' => $entry->getCreated_By(),
            'modified' => $entry->getModified(),
            'modified_by' => $entry->getModified_By(),
            'order' => $entry->getOrder(),
            'status' => $entry->getStatus(),
            'area_id' => $entry->getArea_Id(),
            'legal_capital' => $entry->getLegal_Capital(),
            'experience' => $entry->getExperience(),
            'ability_archive' => $entry->getAbility_Archive(),
            'ability_transport' => $entry->getAbility_Transport(),
            'person' => $entry->getPerson(),
            'agent_status' => $entry->getAgent_Status(),
            'charges_sell' => $entry->getCharges_Sell(),
            'tax_code'=>$entry->getTax_Code(),
            'identity_card'=>$entry->getIdentity_Card(),
            'birthday'=>$entry->getBirthday(),
            'ability_sell' => $entry->getAbility_Sell()
        );
         if (null === ($id = $entry->getId())) {
            unset($data["id"]);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array("id = ?" => $id));
        }
    }
     public function delete($id){
        $this->getDbTable()->delete("id=".$id);
    }
    public function find($id, Model_Agent $entry){
        $result = $this->getDbTable()->find($id);
        if(0== count($result))
        {
            return;
        }
        $row=$result->current();
        $entry->setId($row->id)
                ->setId($row->id)
                ->setName($row->name)
                ->setAddress($row->address)
                ->setProvince_Id($row->province_id)
                ->setDistrict_Id($row->district_id)
                ->setAgent_Cat_Id($row->agent_cat_id)
                ->setPhone($row->phone)
                ->setRepresentavie($row->representavie)
                ->setUser_Id($row->user_id)
                ->setArea_Id($row->area_id)
                ->setCharges($row->charges)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setOrder($row->order)
                ->setStatus($row->status)
                ->setLegal_Capital($row->legal_capital)
                ->setExperience($row->experience)
                ->setAbility_Archive($row->ability_archive)
                ->setAbility_Transport($row->ability_transport)
                ->setPerson($row->person)
                ->setAgent_Status($row->agent_status)
                ->setCharges_Sell($row->charges_sell)
                ->setTax_Code($row->tax_code)
                ->setIdentity_Card($row->identity_card)
                ->setBirthday($row->birthday)
                ->setAbility_Sell($row->ability_sell);
    }

    public function fetchAll(){
        $select = $this->getDbTable()->select();    
        $select->order(array('agent_status'));        
        $result = $this->getDbTable()->fetchAll($select);        
       
        $entries = array();
        foreach ($result as $row){
            $entry = new Model_Agent();
            $entry->setId($row->id)
               ->setId($row->id)
                    ->setName($row->name)
                    ->setAddress($row->address)
                    ->setProvince_Id($row->province_id)
                    ->setDistrict_Id($row->district_id)
                    ->setAgent_Cat_Id($row->agent_cat_id)
                    ->setPhone($row->phone)
                    ->setRepresentavie($row->representavie)
                    ->setUser_Id($row->user_id)
                    ->setArea_Id($row->area_id)
                    ->setCharges($row->charges)
                    ->setCreated($row->created)
                    ->setCreated_By($row->created_by)
                    ->setModified($row->modified)
                    ->setModified_By($row->modified_by)
                    ->setOrder($row->order)
                    ->setStatus($row->status)
                    ->setLegal_Capital($row->legal_capital)
                    ->setExperience($row->experience)
                    ->setAbility_Archive($row->ability_archive)
                    ->setAbility_Transport($row->ability_transport)
                    ->setPerson($row->person)
                    ->setAgent_Status($row->agent_status)
                    ->setCharges_Sell($row->charges_sell)
                    ->setTax_Code($row->tax_code)
                    ->setIdentity_Card($row->identity_card)
                    ->setBirthday($row->birthday)
                    ->setAbility_Sell($row->ability_sell);
            $entries[] = $entry;
        }
        return $entries;
    }
    public function fetchAllArray()
    {
		$data = array(array());
        $resultSet = $this->getDbTable()->fetchAll(PDO::FETCH_COLUMN);
		$count = count($resultSet);
		for($i=0;$i<$count;$i++)
		{
			$data[$resultSet[$i]['id']]['name'] = $resultSet[$i]['name'];
		}
		return $data;
    }
    public function fetchPaging($page, $pageSize, $orderBy, $orderDirection, &$totalRecord, $filters=null)
    {
        $db = Zend_Db_Table::getDefaultAdapter();	
		$aFilterValues = array();
		$sFilter = $this->createWhereCondition($filters, $aFilterValues);
		$queryTotal = "SELECT count(*) as totals FROM agent ".$sFilter."";
		$totalRecord = $this->getTotalRecords($queryTotal, $aFilterValues);
		$query = "SELECT * FROM agent ".$sFilter." order by ".$orderBy." ".$orderDirection." limit ".($page-1)*$pageSize.",".$pageSize."";
        $stmt = $db->query($query, $aFilterValues);		
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Agent();
            $entry->setId($row->id)
                ->setId($row->id)
                ->setName($row->name)
                ->setAddress($row->address)
                ->setProvince_Id($row->province_id)
                ->setDistrict_Id($row->district_id)
                ->setAgent_Cat_Id($row->agent_cat_id)
                ->setPhone($row->phone)
                ->setRepresentavie($row->representavie)
                ->setUser_Id($row->user_id)
                ->setArea_Id($row->area_id)
                ->setCharges($row->charges)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setOrder($row->order)
                ->setStatus($row->status)
                ->setLegal_Capital($row->legal_capital)
                ->setExperience($row->experience)
                ->setAbility_Archive($row->ability_archive)
                ->setAbility_Transport($row->ability_transport)
                ->setPerson($row->person)
                ->setAgent_Status($row->agent_status)
                ->setCharges_Sell($row->charges_sell)
                ->setTax_Code($row->tax_code)
                ->setIdentity_Card($row->identity_card)
                    ->setBirthday($row->birthday)
                    ->setAbility_Sell($row->ability_sell);
            $entries[] = $entry;
        }
        return $entries;
    }
    public function createCombo($id=0, $name='id', $width=230)
	{
		$items = $this->fetchAll();
		$html = '<select name="'.$name.'" style="width:'.$width.'px">';
		$html .= '<option value="0">--Please select--</option>';
		foreach ($items as $item)
		{
			if($id == $item->getId())
			{
				$html .= '<option value="'.$item->getId().'"  selected="selected">'.$item->getName().'</option>';
			}else{
				$html .= '<option value="'.$item->getId().'">'.$item->getName().'</option>';
			}
		}
		$html .= '</select>';
		return $html;
	}
}
